package com.example.yy.startproject_318yy.model;

import com.example.yy.startproject_318yy.bean.WeatherResult;
import com.example.yy.startproject_318yy.iface.WeatherListener;
import com.example.yy.startproject_318yy.iface.Wertheriface;
import com.example.yy.startproject_318yy.service.Weather51Service;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by yy on 2017/3/14.
 */

public class Weather51Model implements Wertheriface{
    private Weather51Service service;
    private Retrofit retrofit;
    private String BASEURL = "http://weather.51wnl.com/weatherinfo/";

    public Weather51Model() {
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getWerther(String cityNumber, final WeatherListener listener) {
        Weather51Service weather51Service = retrofit.create(Weather51Service.class);
        Call<WeatherResult> call=weather51Service.getResult(cityNumber, 1);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(response.body().getWeatherinfonfo());
                }else{
                    listener.onFail("onResponse error");
                }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());

            }
        });

    }
}
